import { Quaternion, Euler } from "three"
export default function createDragAndRotate(currentDom, configEvent) {
    // 鼠标交互变量
    let isDragging = false;
    let previousMousePosition = { x: 0, y: 0 };
    const rotationSpeed = 0.008;
    let domRotate = []
    // 鼠标事件处理函数
    function onMouseDown(event) {
        // 只响应左键点击
        if (event.button !== 0) return;

        isDragging = true;
        previousMousePosition = {
            x: event.clientX,
            y: event.clientY
        };
    }

    function onTouchstart(event) {
        if (event.touches.length > 1) return;
         // 获取第一个触摸点
        const touch = event.touches[0];
        isDragging = true;
        previousMousePosition = {
            x: touch.clientX,
            y: touch.clientY
        };
        // 阻止默认行为（如页面滚动）
        event.preventDefault();
    }

    function onMouseMove(event) {
        if (!(isDragging)) return;

        // 计算鼠标移动增量
        const deltaMove = {
            x: event.clientX - previousMousePosition.x,
            y: event.clientY - previousMousePosition.y
        };

        domRotate.forEach(item => {
            // 使用四元数实现增量旋转（避免万向锁）
            const deltaQuaternion = new Quaternion()
                .setFromEuler(new Euler(
                    deltaMove.y * rotationSpeed,
                    deltaMove.x * rotationSpeed,
                    0,
                    'XYZ'
                ));

            // 累加旋转（关键点：以当前旋转状态为基准）
            item.quaternion.multiplyQuaternions(deltaQuaternion, item.quaternion);
        })

        // 更新前一个鼠标位置
        previousMousePosition = {
            x: event.clientX,
            y: event.clientY
        };
        configEvent && configEvent.dragRotate()
    }
    function onTouchmove(event) {
        if (!(isDragging)) return;
        if (event.touches.length > 1) return;
        // 获取第一个触摸点
        const touch = event.touches[0];

        // 计算鼠标移动增量
        const deltaMove = {
            x: touch.clientX - previousMousePosition.x,
            y: touch.clientY - previousMousePosition.y
        };

        domRotate.forEach(item => {
            // 使用四元数实现增量旋转（避免万向锁）
            const deltaQuaternion = new Quaternion()
                .setFromEuler(new Euler(
                    deltaMove.y * rotationSpeed,
                    deltaMove.x * rotationSpeed,
                    0,
                    'XYZ'
                ));

            // 累加旋转（关键点：以当前旋转状态为基准）
            item.quaternion.multiplyQuaternions(deltaQuaternion, item.quaternion);
        })

        // 更新前一个鼠标位置
        previousMousePosition = {
            x: touch.clientX,
            y: touch.clientY
        };
        configEvent && configEvent.dragRotate()
    }

    function onMouseUp() {
        isDragging = false;
    }

    function onMouseOut() {
        isDragging = false;
    }


    function handleEventListener(bool) {
        if (bool) {
            // 添加事件监听
            console.log("添加事件监听")
            // currentDom.value.addEventListener('mousedown', onMouseDown, false);
            // currentDom.value.addEventListener('mousemove', onMouseMove, false);
            // currentDom.value.addEventListener('mouseup', onMouseUp, false);
            // currentDom.value.addEventListener('mouseout', onMouseOut, false);

            currentDom.value.addEventListener('pointerdown', onMouseDown, false);
            currentDom.value.addEventListener('pointermove', onMouseMove, false);
            currentDom.value.addEventListener('pointerup', onMouseUp, false);
            currentDom.value.addEventListener('pointercancel', onMouseOut, false);
            // 触摸事件
            currentDom.value.addEventListener('touchstart', onTouchstart, { passive: false })
            currentDom.value.addEventListener('touchmove', onTouchmove, { passive: false })
            currentDom.value.addEventListener('touchend', onMouseUp, { passive: false })
        } else {
            console.log("移除监听事件")
            currentDom.value.removeEventListener('mousedown', null);
            currentDom.value.removeEventListener('mousemove', null);
            currentDom.value.removeEventListener('mouseup', null);
            currentDom.value.removeEventListener('mouseout', null);
        }
    }

    function addRotate(model) {
        domRotate.push(model)
    }

    handleEventListener(true)

    return {
        addRotate,
        handleEventListener
    }
};
